<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="更新渠道">
          <el-select v-model="form.platform" placeholder="更新渠道">
            <el-option label="Google Play" value="google" />
            <el-option label="Metafun官网APK" value="metafun" />
            <el-option label="Apple AppStore" value="appstore" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="platform"
            label="渠道"
            align="center"
          />
          <el-table-column
            prop="version"
            label="版本号"
            align="center"
          />
          <el-table-column
            prop="version_code"
            label="版本Code"
            align="center"
          />
          <el-table-column
            prop="version_log"
            label="更新内容"
            align="center"
            width="260"
          />
          <el-table-column
            prop="update_date"
            label="更新日期"
            align="center"
            width="120"
          />
          <el-table-column
            prop="is_force"
            label="是否强更"
            align="center"
            :formatter="formatter"
          />
          <el-table-column
            prop="size"
            label="包大小"
            align="center"
          />
          <el-table-column
            prop="min_code"
            label="最小Code"
            align="center"
          />
          <el-table-column
            prop="max_code"
            label="最大Code"
            align="center"
          />
          <el-table-column
            prop="apk_url"
            label="APK地址"
            align="center"
            width="180"
          />
          <el-table-column
            prop="google_play_url"
            label="GooglePlay地址"
            align="center"
            width="180"
          />
          <el-table-column
            prop="app_store_url"
            label="AppStore地址"
            align="center"
            width="180"
          />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, formatter } from "@/utils/utils";
import { upgradeListApi, upgradeOperateApi } from "@/api/moby/upgrade/upgrade";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);

const formData = {
  platform: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  upgrade_id: "",
  platform: "",
  version: "",
  version_log: "",
  update_date: "",
  size: "",
  version_code: "",
  min_code: "",
  max_code: "",
  is_force: "",
  apk_url: "",
  google_play_url: "",
  app_store_url: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    platform: form.platform,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await upgradeListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogVisible.value = true;
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    Object.assign(dialogForm, { ...row, upgrade_id: row.id });
    dialogVisible.value = true;
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await upgradeOperateApi(param);

    ElMessage({
      message: res.message,
      type: "success",
    });

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
